using System;
using System.Collections.Generic;
using System.Text;

namespace SymAutomate.PcInterfaceControl
{
    /// <summary> 
    /// Error Numbers 400-419; Reference file ErrorNumbers.txt for more information.
    /// 400 Error: One of the arguments 'jobfilename' or 'reportsequencelist' must be specified. Use /h switch for help.
    /// 401 Error: Required argument 'ftpserverip' missing. Use /h switch for help.
    /// 402 Error: Required argument 'ftpserverusername' missing. Use /h switch for help.
    /// 403 Error: Required argument 'ftpserverpassword' missing. Use /h switch for help.
    /// 404 Error: Invalid value for argument 'ftpserverpassword'. Valid values are ascii, binary.
    /// 405 Error: Required argument 'filename' missing. Use /h switch for help. 
    /// 406 Error: Required argument 'ftpservertimoutinsec' missing or not greater than 0. Use /h switch for help.
    /// 407 Error: Invalid format for argument 'reportsequencelist'. %message%.
    /// 408 Error: Only one of the arguments 'jobfilename' or 'reportsequencelist' should be specified. Use /h switch for help.
    /// 409 Error: Required argument 'ftpservertimoutinsec' missing or could not be parsed. %ExceptionMessage%.
    /// </summary>
    class Parameters : ParametersBase
    {
        #region Class Variables and Access Methods
        private bool __EncryptSensitiveKeys;
        private string __EncryptionKey;
        private string __FtpServerIp;
        private string __FtpServerUsername;
        private string __FtpServerPassword;
        private int __FtpServerTimoutoutInSec;
        private string __FtpServerTransferMode;
        private string __FtpServerFolderPath;
        private string __FtpDestinationFileNameTemplate;
        private string __FtpJobFileReportTitle;
        private string __JobFileName;
        private string __ReportSequenceList;
        private string __FileName;

        public string FtpServerIp { get { return __FtpServerIp; } }
        public string FtpServerUsername { get { return __FtpServerUsername; } }
        public string FtpServerPassword { get { return __FtpServerPassword; } }
        public int FtpServerTimoutoutInSec { get { return __FtpServerTimoutoutInSec; } }
        public string FtpServerTransferMode { get { return __FtpServerTransferMode; } }
        public string FtpServerFolderName { get { return __FtpServerFolderPath; } }
        public string FtpDestinationFileNameTemplate { get { return __FtpDestinationFileNameTemplate; } }
        public string FtpJobFileReportTitle { get { return __FtpJobFileReportTitle; } }
        public string JobFileName { get { return __JobFileName; } }
        public string ReportSequenceList { get { return __ReportSequenceList; } }
        public string FileName { get { return __FileName; } }
        #endregion

        public Parameters(ConsoleArguments _ConsoleArguments, List<ConsoleHelp> _ConsoleHelpList)
            : base(_ConsoleArguments, _ConsoleHelpList)
        {
            //defaults
            __FtpServerTimoutoutInSec = 3;

            //read from config file
            __EncryptSensitiveKeys = System.Configuration.ConfigurationManager.AppSettings["encryptsensitivekeys"] == "true" ? true : false;
            __EncryptionKey = Util.Driver.ReadRegistryKey(@"HKEY_LOCAL_MACHINE\Software\SymAutomate", "EncryptionKey");
            __FtpServerIp = System.Configuration.ConfigurationManager.AppSettings["ftpserverip"];
            __FtpServerUsername = System.Configuration.ConfigurationManager.AppSettings["ftpserverusername"];
            __FtpServerPassword = System.Configuration.ConfigurationManager.AppSettings["ftpserverpassword"];
            try
            {
                __FtpServerTimoutoutInSec = GlobalFunctions.GetIntValue(System.Configuration.ConfigurationManager.AppSettings["ftpservertimoutinsec"].ToString());
            }
            catch (Exception _ArgumentException)
            {
                throw new ReturnException("409 Error: Required argument 'ftpservertimoutinsec' missing or could not be parsed. " + _ArgumentException.Message + ".");
            }
            __FtpServerTransferMode = System.Configuration.ConfigurationManager.AppSettings["ftpservertransfermode"];

            #region Decrypt the values if encryption is enabled
            if (__EncryptSensitiveKeys && !string.IsNullOrEmpty(__EncryptionKey))
            {
                AESEncryption _AESEncryption = new AESEncryption(__EncryptionKey);
                try
                {
                    __FtpServerPassword = _AESEncryption.Decrypt(__FtpServerPassword);
                }
                catch (Exception _DecyptionException)
                {
                    throw new ReturnException("210 Error: Encrypted 'ftpserverpassword' from the config file could not be decrypted.");
                }
            }
            #endregion

            //save from command line arguments
            if (_ConsoleArguments.ArgumentCollection.Contains("ftpserverip")) __FtpServerIp = _ConsoleArguments.ArgumentCollection["ftpserverip"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("ftpserverusername")) __FtpServerUsername = _ConsoleArguments.ArgumentCollection["ftpserverusername"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("ftpserverpassword")) __FtpServerPassword = _ConsoleArguments.ArgumentCollection["ftpserverpassword"].ToString();
            try
            {
                if (_ConsoleArguments.ArgumentCollection.Contains("ftpservertimoutinsec")) __FtpServerTimoutoutInSec = GlobalFunctions.GetIntValue(_ConsoleArguments.ArgumentCollection["ftpservertimoutinsec"].ToString());
            }
            catch (Exception _ArgumentException)
            {
                throw new ReturnException("409 Error: Required argument 'ftpservertimoutinsec' missing or could not be parsed. " + _ArgumentException.Message + ".");
            }
            if (_ConsoleArguments.ArgumentCollection.Contains("ftpservertransfermode")) __FtpServerTransferMode = _ConsoleArguments.ArgumentCollection["ftpservertransfermode"].ToString().ToLower();
            if (_ConsoleArguments.ArgumentCollection.Contains("ftpserverfolderpath")) __FtpServerFolderPath = _ConsoleArguments.ArgumentCollection["ftpserverfolderpath"].ToString().ToLower();       
            if (_ConsoleArguments.ArgumentCollection.Contains("ftpdestinationfilenametemplate")) __FtpDestinationFileNameTemplate = _ConsoleArguments.ArgumentCollection["ftpdestinationfilenametemplate"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("ftpjobfilereporttitle")) __FtpJobFileReportTitle = _ConsoleArguments.ArgumentCollection["ftpjobfilereporttitle"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("jobfilename")) __JobFileName = _ConsoleArguments.ArgumentCollection["jobfilename"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("reportsequencelist")) __ReportSequenceList = _ConsoleArguments.ArgumentCollection["reportsequencelist"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("filename")) __FileName = _ConsoleArguments.ArgumentCollection["filename"].ToString();
           

            //perform argument error checking
            if (string.IsNullOrEmpty(__FtpServerIp))
                throw new ReturnException("401 Error: Required argument 'ftpserverip' missing. Use /h switch for help.");
            if (string.IsNullOrEmpty(__FtpServerUsername))
                throw new ReturnException("402 Error: Required argument 'ftpserverusername' missing. Use /h switch for help.");
            if (string.IsNullOrEmpty(__FtpServerPassword))
                throw new ReturnException("403 Error: Required argument 'ftpserverpassword' missing. Use /h switch for help.");
            if (__FtpServerTimoutoutInSec <= 0)
                throw new ReturnException("406 Error: Required argument 'ftpservertimoutinsec' missing or not greater than 0. Use /h switch for help.");
            if (string.IsNullOrEmpty(__FtpServerTransferMode) || (__FtpServerTransferMode != "ascii" && __FtpServerTransferMode != "binary"))
                throw new ReturnException("404 Error: Invalid value for argument 'ftpservertransfermode'. Valid values are ascii, binary.");
            if (!string.IsNullOrEmpty(__FtpServerFolderPath) && !__FtpServerFolderPath.EndsWith("/"))
                __FtpServerFolderPath += "/";
            if (_ConsoleArguments.ArgumentCollection["command"].ToString().ToLower() == "ReportFileToPc".ToLower())
            {
                if (string.IsNullOrEmpty(__JobFileName) && string.IsNullOrEmpty(__ReportSequenceList))
                    throw new ReturnException("400 Error: One of the arguments 'jobfilename' or 'reportsequencelist' must be specified. Use /h switch for help.");
                if (!string.IsNullOrEmpty(__JobFileName) && !string.IsNullOrEmpty(__ReportSequenceList))
                    throw new ReturnException("408 Error: Only one of the arguments 'jobfilename' or 'reportsequencelist' should be specified. Use /h switch for help.");  
                if (!string.IsNullOrEmpty(__ReportSequenceList))
                {
                    List<string> _ReportsListDiscard = new List<string>();
                    string _Error = Driver.ValidateReportSequenceList(__ReportSequenceList, ref _ReportsListDiscard);
                    if (_Error != null)
                        throw new ReturnException("407 Error: Invalid format for argument 'reportsequencelist'. " + _Error + ".");
                }
            }
            if (_ConsoleArguments.ArgumentCollection["command"].ToString().ToLower() == "LetterFileToPc".ToLower())
            {
                if (string.IsNullOrEmpty(__FileName))
                    throw new ReturnException("405 Error: Required argument 'filename' missing. Use /h switch for help.");
            }
            if (_ConsoleArguments.ArgumentCollection["command"].ToString().ToLower() == "DataFileToPc".ToLower())
            {
                if (string.IsNullOrEmpty(__FileName))
                    throw new ReturnException("405 Error: Required argument 'filename' missing. Use /h switch for help.");
            }
            if (_ConsoleArguments.ArgumentCollection["command"].ToString().ToLower() == "PcToEditFile".ToLower())
            {
                if (string.IsNullOrEmpty(__FileName))
                    throw new ReturnException("405 Error: Required argument 'filename' missing. Use /h switch for help.");
            }
            if (_ConsoleArguments.ArgumentCollection["command"].ToString().ToLower() == "PcToLetterFile".ToLower())
            {
                if (string.IsNullOrEmpty(__FileName))
                    throw new ReturnException("405 Error: Required argument 'filename' missing. Use /h switch for help.");
            }
            if (_ConsoleArguments.ArgumentCollection["command"].ToString().ToLower() == "PcToDataFile".ToLower())
            {
                if (string.IsNullOrEmpty(__FileName))
                    throw new ReturnException("405 Error: Required argument 'filename' missing. Use /h switch for help.");
            }
        }
    }
}
